﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

using CentralOffice.Task.Data;
using CentralOffice.Task.BusinessActions;


using ApprovalTable = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsDataTable;
using ApprovalRow = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsRow;
using Husb.Util;
using Husb.Util.Enum;
using System.Data;


namespace CentralOffice.WebClient.Tasks
{
    public partial class ApplicationApprove : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        protected void odsApproval_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            int status = EnumUtil.GetInt32(SiteConstants.ApprovalStatus, CategoryDataSourceType.Xml, "通过");

            IOrderedDictionary parameters = e.InputParameters;
            ApprovalRow approval = (new ApprovalDataSet()).Approvals.NewApprovalsRow();
            approval.ApprovedBy = CommonUtil.TrimAll(ApplicationContext.ActualUserName);
            //parameters["whereClause"] = task.FilterExpression;
            parameters["whereClause"] = approval.FilterExpression + "  AND Status < " + status.ToString() + " AND ApplicationIsDeleted = 0 AND DateApproved IS NULL ";
            //parameters.Remove("Id");

            //parameters["enableCache"] = "CreatedTime"; 
        }
        protected void odsApproved_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            //int status = EnumUtil.GetInt32(SiteConstants.ApprovalStatus, CategoryDataSourceType.Xml, "通过");

            IOrderedDictionary parameters = e.InputParameters;
            ApprovalRow approval = (new ApprovalDataSet()).Approvals.NewApprovalsRow();
            approval.ApprovedBy = CommonUtil.TrimAll(CommonUtil.TrimAll(ApplicationContext.ActualUserName));
            //parameters["whereClause"] = task.FilterExpression;
            //parameters["whereClause"] = approval.FilterExpression + "  AND Status < " + status.ToString() + " AND DateApproved IS NULL ";
            //parameters.Remove("Id");
            parameters["whereClause"] = approval.FilterExpression + " AND ApplicationIsDeleted = 0 AND DateApproved IS NOT NULL ";
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ApprovalRow approval = ((DataRowView)e.Row.DataItem).Row as ApprovalRow;

                SiteUtil.SetStatusColor(e, 5, approval.ResultApproved);

                if (approval[(approval.Table as ApprovalTable).PreviousDateApprovedColumn.ColumnName] != DBNull.Value)
                {
                    SiteUtil.SetStatusColor(e, 7, approval.PreviousResultApproved);
                }
                
                if (!approval.IsNextStepIsApprovedNull() && approval.NextStepIsApproved)
                {
                    e.Row.Cells[gv.Columns.Count - 1].Controls[0].Visible = false;
                }
                //SiteUtil.SetElapsedTime(e, 8, approval.IsElapsedTimeNull() ? 0 : approval.ElapsedTime);
                //e.Row.Cells[8].Text = SiteUtil.GetDateTimeString((approval.IsDateApprovedNull() ? DateTime.Now : approval.DateApproved) - approval.CreatedTime);
                DateTime approvedDate = approval.IsDateApprovedNull() ? DateTime.Now : approval.DateApproved;
                e.Row.Cells[8].Text = SiteUtil.GetDateTimeString(approvedDate - approval.CreatedTime, (approval.IsWarningDateNull() ? DateTime.Now: approval.WarningDate) < approvedDate);
            }
        }

        protected void gvApproval_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ApprovalRow approval = ((DataRowView)e.Row.DataItem).Row as ApprovalRow;

                if (approval[(approval.Table as ApprovalTable).PreviousDateApprovedColumn.ColumnName] != DBNull.Value)
                {
                    SiteUtil.SetStatusColor(e, 8, approval.PreviousResultApproved);
                }


                //e.Row.Cells[9].Text = SiteUtil.GetDateTimeString((approval.IsDateApprovedNull() ? DateTime.Now : approval.DateApproved) - approval.CreatedTime);

                //SiteUtil.SetElapsedTime(e, 9, approval.IsElapsedTimeNull() ? 0 : approval.ElapsedTime);
                DateTime approvedDate = approval.IsDateApprovedNull() ? DateTime.Now : approval.DateApproved;
                
                //e.Row.Cells[9].Text = SiteUtil.GetDateTimeString(approvedDate - approval.CreatedTime, (approval.IsWarningDateNull() ? DateTime.Now.AddSeconds(-1) : approval.WarningDate) < approvedDate);

                e.Row.Cells[9].Text = SiteUtil.GetDateTimeString(approvedDate - approval.CreatedTime, (approval.IsWarningDateNull() ? false : approval.WarningDate < approvedDate));
            }
        }
        
    }
}
